package com.example.calculator;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

//数据模型
public class MyViewModel extends ViewModel{
    private MutableLiveData<String>mainNum;//主数值（用户正在操作的数）

    public String num[]={"",""};//储存参与计算的数值
    public String sign1 = "",sign2 = "";//储存运算符号
    public boolean havePoint = false;//主数值是否包含小数点

    public MutableLiveData<String>getMainNum(){
        if(mainNum == null){
            mainNum = new MutableLiveData<>();
            mainNum.setValue("0");

        }
        return mainNum;
    }

    public void setMainNum(String n){
        if(mainNum.getValue().equals("0")){
            mainNum.setValue(n);
        }else {
            mainNum.setValue(mainNum.getValue() + n);
        }
    }
    public String mainNumWithNum_0_Tocal(){
        String value="0";

        if(mainNum.getValue().contains(".") || num[0].contains(".")){//如果两个有一个是小数点的话
            switch (sign1){
                case"+":
                    value = String.valueOf( Double.valueOf(num[0]) + Double.valueOf(mainNum.getValue()) );
                    break;
                case "-":
                    value = String.valueOf( Double.valueOf(num[0]) - Double.valueOf(mainNum.getValue()) );
                    break;
                case "*":
                    value = String.valueOf( Double.valueOf(num[0]) * Double.valueOf(mainNum.getValue()) );
                    break;
                case"/":
                    if(mainNum.getValue().equals("0")){
                        mainNum.setValue("1");
                    }
                    value = String.valueOf( Double.valueOf(num[0]) / Double.valueOf(mainNum.getValue()) );
                    break;
            }

        }else {
            switch (sign1){
                case"+":
                    value = String.valueOf( Integer.valueOf(num[0]) + Integer.valueOf(mainNum.getValue()) );
                    break;
                case "-":
                    value = String.valueOf( Integer.valueOf(num[0]) - Integer.valueOf(mainNum.getValue()) );
                    break;
                case "*":
                    value = String.valueOf( Integer.valueOf(num[0]) * Integer.valueOf(mainNum.getValue()) );
                    break;
                case"/":
                    if(mainNum.getValue().equals("0")){
                        mainNum.setValue("1");
                    }
                    value = String.valueOf( Double.valueOf(num[0]) / Double.valueOf(mainNum.getValue()) );
                    break;
            }

        }
        return value;
    }
    public String mainNumWithNum_1_Tocal() {
        String value = "0";

        if (mainNum.getValue().contains(".") || num[1].contains(".")) {//如果两个有一个是小数点的话
            switch (sign2) {
                case "*":
                    value = String.valueOf(Double.valueOf(num[1]) * Double.valueOf(mainNum.getValue()));
                    break;
                case "/":
                    if (mainNum.getValue().equals("0")) {
                        mainNum.setValue("1");
                    }
                    value = String.valueOf(Double.valueOf(num[1]) / Double.valueOf(mainNum.getValue()));
                    break;
            }

        } else {
            switch (sign2) {
                case "*":
                    value = String.valueOf(Integer.valueOf(num[1]) * Integer.valueOf(mainNum.getValue()));
                    break;
                case "/":
                    if (mainNum.getValue().equals("0")) {
                        mainNum.setValue("1");
                    }
                    value = String.valueOf(Double.valueOf(num[1]) / Double.valueOf(mainNum.getValue()));
                    break;
            }

        }
        return value;
    }
}
